<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">

                <template v-slot="{row} ">
                  <el-button size="small" type="success" @click="hAssign(row.id) ">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="del(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="q.page"
                :page-sizes="[2,5, 7,12]"
                :page-size="q.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 新增弹框 -->
    <el-dialog
      :title="isEsit? '编辑': '添加' "
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      @closed="hlosed"
    >
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small">取消</el-button>
          <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <el-dialog
      title="添加权限"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialogAssign"
    >
      <assignPermission v-if="showDialogAssign" :id="curId" @close="success" />
    </el-dialog>
  </div>
</template>
<script>
import { addRoles, delRoles, getRoles, updataRoles } from '@/api/role'
import assignPermission from './assignPermission.vue'
export default {
  components: { assignPermission },
  data() {
    return {
      showDialog: false,
      showDialogAssign: false,
      list: [],
      curId: '',
      isEsit: false,
      q: {
        page: 1,
        pagesize: 2
      },
      total: 0,

      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    // 获取所有角色列表
    this.loadRole()
  },
  methods: {

    async loadRole() {
      try {
        const res = await getRoles(this.q)
        this.list = res.data.rows
        this.total = res.data.total
      } catch (e) {
        console.log(e)
      }
    },
    // 一页显示多少条数据
    handleSizeChange(pagesize) {
      // 将当前页条数重新设置给q.pagesize
      this.q.pagesize = pagesize
      this.loadRole()
    },
    // 触发分页事件
    handleCurrentChange(page) {
      // 将当前页重新设置给q.page
      this.q.page = page
      this.loadRole()
    },
    // 删除
    async  del(id) {
      const result = await this.$confirm('真的要删除吗', '提示', { type: 'warning' }).catch(e => e)
      if (result !== 'confirm') return
      try {
        if (this.list.length === 1 && this.q.page > 1) {
          this.q.page--
        }
        const res = await delRoles(id)
        this.$message.success(res.message)
        this.loadRole()
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 添加
    hAdd() {
      this.showDialog = true
      this.isEsit = false
    },
    // 编辑
    hEdit(row) {
      this.isEsit = true
      this.showDialog = true
      this.roleForm = { ...row }
    },
    // 利用promise优化代码
    async hSubmit() {
      this.isEsit ? this.doEidt() : this.doAdd()
      const valid = await this.$refs.roleForm.validate().catch(e => e)
      if (!valid) return
    },
    async  doAdd() {
      const res = await addRoles(this.roleForm).catch(e => e)
      if (res.code !== 10000) return this.$message.error(res.message)
      this.$message.success(res.message)
      if (this.total % this.q.pagesize === 0) {
        this.total++
      }
      // 需求:添加数据时,如果当前页限定的条数已经满了,就新增一页,跳到最后一页
      // 思路:用当前总条数除以每页的条数,得到当前有多少页,如果新增数据,当前页的数据已经满了
      //      就向上取整,新增的数据,添加到新的页数上
      this.q.page = Math.ceil(this.total / this.q.pagesize)
      this.showDialog = false
      this.loadRole()
    },
    async doEidt() {
      const res = await updataRoles(this.roleForm).catch(e => e)
      if (res.code !== 10000) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.showDialog = false
      this.loadRole()
    },
    hlosed() {
      this.roleForm.name = ''
      this.roleForm.description = ''
      this.$refs.roleForm.clearValidate()
    },
    async  hAssign(id) {
      this.showDialogAssign = true
      this.curId = id
    },
    success() {
      this.showDialogAssign = false
    }
  }
}
</script>

